﻿package scrn;


import core.*;
import javax.microedition.lcdui.*;

import mess.*;


// ============================================================== Экран настроек
public class ScrSett extends Form implements CommandListener, Win {


//=================================================================== Переменные
// Команды
private Command comSave = new Command("Сохранить", Command.OK, 1);
private Command comCancel = new Command("Отменить", Command.CANCEL, 1);

// Поля
private StringItem itLogin;
private StringItem itPass;
private ChoiceGroup itChoice;
//private TextField itServer = new TextField("Сервер", Setting.server, 32,
//		TextField.URL);
//private TextField itPort = new TextField("Порт", "" + Setting.port, 6,
//		TextField.NUMERIC);
private Gauge itVolume;

private TextField itTimeSer;
private TextField itTimeLoc;

//================================================================ Инициализация
public ScrSett() {

	super("Настройки");

	Mess.m("Setting.init", "=====");

	itLogin = new StringItem("Ваш логин", Setting.login, Item.PLAIN);
	itLogin.setLayout(Item.LAYOUT_NEWLINE_AFTER | Item.LAYOUT_LEFT);
	itPass = new StringItem("Пароль", Setting.password, Item.PLAIN);
	itPass.setLayout(Item.LAYOUT_NEWLINE_AFTER);

	itChoice = new ChoiceGroup(null, Choice.MULTIPLE);
	itVolume = new Gauge("Громкость", true, 10, Setting.volume / 10);
	itTimeSer = new TextField("Время сервера +", "" + Setting.timeSerShift, 4,
			TextField.NUMERIC);
	itTimeLoc = new TextField("Мое время +", "" + Setting.timeLocShift, 4,
			TextField.NUMERIC);


	addCommand(comSave);
	addCommand(comCancel);

	setCommandListener(this);

	try {
		this.append(new ImageItem("", Image.createImage("/res/logo.png"),
				ImageItem.LAYOUT_CENTER | ImageItem.LAYOUT_NEWLINE_AFTER, "logo"));

		this.append(itLogin);
		this.append(itPass);
		this.append(itVolume);

		itChoice.append("Вибрация", null);
		itChoice.setSelectedIndex(0, Setting.fVibra);
		//		itChoice.append("Вести лог действий", null);
		//		itChoice.setSelectedIndex(1, Setting.fLog);
		//itChoice.append("Время в сообщениях", null);
		//itChoice.setSelectedIndex(1, Setting.fTime);
		//itChoice.append("Активироваться при сообщении", null);
		//itChoice.setSelectedIndex(2, Setting.fShow);
		//itChoice.append("Проверка доставки", null);
		//itChoice.setSelectedIndex(3, Setting.fRes);
		//itChoice.append("Анимировать смайлы", null);
		//itChoice.setSelectedIndex(4, Setting.fAnimate);
		this.append(itChoice);

		//		this.append(itServer);
		//		this.append(itPort);

		this.append(itTimeSer);
		this.append(itTimeLoc);

		this.append("Версия МолЧат: " + Setting.VER + "\n");
	} catch (Exception e) {
		Mess.err("ScrSett.init", e);
	}
}


//========================================================== Сохранить настройки
public void saveSettings() {

	if (Setting.volume != itVolume.getValue() * 10) { // Изменилась настройка звука
		Setting.volume = itVolume.getValue() * 10;
		Man.soundMan.setLevel(itVolume.getValue() * 10);
	}

	Setting.fVibra = itChoice.isSelected(0);
	//	Setting.fLog = itChoice.isSelected(1);

	//	boolean fChng = false;
	//	if (!Setting.login.equals(itLogin.getString())
	//			|| !(Setting.password.equals(itPass.getString()))) {
	//		fChng = true;
	//	}
	//
	//	Setting.login = itLogin.getString();
	//	Setting.password = itPass.getString();
	//	Setting.server = itServer.getString();
	//	Setting.port = Integer.parseInt(itPort.getString());

	Setting.timeSerShift = Integer.parseInt(itTimeSer.getString());
	Setting.timeLocShift = Integer.parseInt(itTimeLoc.getString());

	Setting.saveCore();
	Setting.saveMain();

	Clock.init();

	//	if (fChng) {
	//		Man.main.connect();
	//	}

}


//============================================= Освободить ресурсы и зкрыть окно
public void close(int from) {

	if (from <= Mess.LOCAL) {
		Man.scrMan.showMain();
	}
}


//============================================================ Обработчик команд
public void commandAction(Command command, Displayable displayable) {

	if (command == comSave) {
		saveSettings();
	}

	close(Mess.LOCAL);
}


public boolean isGraphic() {

	return false;
}


public boolean keyPressed(int keyCode) {

	return false;
}


public void show(Win oldWin) {

}


public boolean chStat(String statText) {

	return false;

	// TODO Auto-generated method stub

}


public void fOnline(boolean fOnline) {

// TODO Auto-generated method stub

}


//============================================= Извещение о пришедшем сообщении
public boolean inText(int contType, int contNum, Message message) {

	this.setTitle(Man.scrMan.getTitStr(contType, contNum, message, 3));
	return false;

}


// ====================================================== Тик системного таймера 
public boolean tick() {

	return false;
	// TODO Auto-generated method stub

}


public boolean keyState(int keyState) {

	// TODO Auto-generated method stub
	return false;
}


public boolean[] getComs() {

	// TODO Auto-generated method stub
	return null;
}


public void command(Command command) {

	// TODO Auto-generated method stub
	
}


}
